package com.guaju.yueshangLife.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.AddAddressActivity;
import com.guaju.yueshangLife.bean.MyAddressBean;
import com.guaju.yueshangLife.databinding.FooterHomeItemBinding;
import com.guaju.yueshangLife.databinding.ItemCancledBinding;
import com.guaju.yueshangLife.databinding.ItemMyAddressBinding;
import com.guaju.yueshangLife.databinding.ItemUncommentBinding;
import com.guaju.yueshangLife.databinding.ItemUnpayBinding;
import com.guaju.yueshangLife.databinding.ItemUnreceivedBinding;

import java.util.List;

public class AddressListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<MyAddressBean.DataBean> list;
    private int status = 1;
    private OnSetDefaultClicked onSetDefaultClicked;
    public static final int LOAD_MORE = 0;
    public static final int LOAD_PULL_TO = 1;
    public static final int LOAD_NONE = 2;
    public static final int LOAD_END = 3;
    private static final int TYPE_FOOTER = -2;
    private static final int TYPE_CONTENT = -4;

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    Activity activity;
    private final LayoutInflater layoutInflater;
    private View view;

    public AddressListAdapter(Context context, List<MyAddressBean.DataBean> list) {
        this.context = context;
        this.list = list;
        activity = (Activity) context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {

        //选择类型
        RecyclerView.ViewHolder result = null;
        switch (type) {
            case TYPE_CONTENT:
                ItemMyAddressBinding addressBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_my_address, parent, false);
                result = new NormalHolder(addressBinding.getRoot());
                break;

            case TYPE_FOOTER:
                FooterHomeItemBinding mBindFooter = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.footer_home_item, parent, false);
                result = new FooterViewHolder(mBindFooter.getRoot());
                break;
        }
        return result;
    }

    //绑定
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            footerViewHolder.bindItem();
        } else if (holder instanceof NormalHolder) {
            NormalHolder normalHolder = (NormalHolder) holder;
            normalHolder.setContent(list.get(position));
        }

    }

    //有多少个item？
    @Override
    public int getItemCount() {
        return list.size();
    }

    //item类型
    @Override
    public int getItemViewType(int position) {
        int type = TYPE_CONTENT;
        return type;
    }


    public interface OnItemClickListener {
        void onItemClick(MyAddressBean.DataBean  dataBean);

        void onItemLongClick(View v);
    }

    public AddressListAdapter.OnItemClickListener mOnItemClickListener;//第二步：声明自定义的接口

    //第三步：定义方法并暴露给外面的调用者
    public void setOnItemClickListener(AddressListAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public class NormalHolder extends RecyclerView.ViewHolder {
        ItemMyAddressBinding addressBinding;
        private final Drawable drawable;
        private String screteTel;

        public NormalHolder(View itemView) {
            super(itemView);
            addressBinding = DataBindingUtil.getBinding(itemView);

            drawable = context.getResources().getDrawable(R.drawable.selector_refund_reason);
            drawable.setBounds(0, 0, 40, 40);
            addressBinding.cbDefaultAddress.setCompoundDrawables(drawable, null, null, null);
        }

        public void setContent(final MyAddressBean.DataBean dataBean) {
            final String address = dataBean.getAddress();
            String contact_name = dataBean.getContact_name();
            String contact_phone = dataBean.getContact_phone();
            int is_default = dataBean.getIs_default();
            addressBinding.tvAddressDetail.setText(address);
            addressBinding.tvReceiveName.setText(contact_name);
            if ((!TextUtils.isEmpty(contact_phone)) && contact_phone.length() == 11) {
                screteTel = contact_phone.substring(0, 3) + "****" + contact_phone.substring(7, 11);
                addressBinding.tvReceiveTel.setText(screteTel);
            } else {
                addressBinding.tvReceiveTel.setText(contact_phone);
            }
            if (is_default == 0) {
                addressBinding.cbDefaultAddress.setCompoundDrawables(drawable, null, null, null);
                addressBinding.cbDefaultAddress.setChecked(false);
            } else {
                addressBinding.cbDefaultAddress.setChecked(true);
                addressBinding.cbDefaultAddress.setCompoundDrawables(null, null, null, null);
                addressBinding.cbDefaultAddress.setText("默认地址");
            }

            addressBinding.cbDefaultAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (addressBinding.cbDefaultAddress.isChecked()) {
                        if (onSetDefaultClicked != null) {
                            onSetDefaultClicked.onSetDefault(dataBean.getId());
                        }
                    }
                }
            });
            addressBinding.llAddressEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(AddAddressActivity.TYPE, 1);
                    Intent intent = new Intent(context, AddAddressActivity.class);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });

            addressBinding.llAddressDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /**
                     * 删除地址
                     */
                }
            });

            addressBinding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mOnItemClickListener.onItemClick(dataBean);
                }
            });
        }


    }

    /**
     * footer view
     */
    private class FooterViewHolder extends RecyclerView.ViewHolder {

        FooterHomeItemBinding mBindFooter;

        FooterViewHolder(View itemView) {
            super(itemView);
            mBindFooter = DataBindingUtil.getBinding(itemView);
            mBindFooter.rlMore.setGravity(Gravity.CENTER);
        }

        private void bindItem() {
            switch (status) {
                case LOAD_MORE:
                    mBindFooter.progress.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setText("正在加载...");
                    itemView.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_PULL_TO:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("上拉加载更多");
                    itemView.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_NONE:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("暂无数据");
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_END:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.GONE);
                    mBindFooter.homeBottom.setVisibility(View.VISIBLE);
//                    itemView.setVisibility(View.GONE);
                default:
                    break;
            }
        }

    }

    /**
     * 这里规定 position =  getItemCount() - 1 时
     * 就为尾布局，设置为占满整屏幕宽
     * getItemCount() 改了 ，这里就不用改
     */
    private boolean isFooter(int position) {
        return position < getItemCount() && position >= getItemCount() - 1;
    }

    public void updateLoadStatus(int status) {
        this.status = status;
        notifyDataSetChanged();
    }

    public int getLoadStatus() {
        return this.status;
    }

    public void setOnSetDefaultClicked(OnSetDefaultClicked onSetDefaultClicked) {
        this.onSetDefaultClicked = onSetDefaultClicked;
    }

    public interface OnSetDefaultClicked {
        void onSetDefault(int id);
    }
}